package cn.zhl.algorithm._1_排序;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-05-27 12:10
 **/
public abstract class Sort {
	private int compareCount;
	private int swapCount;
	protected Integer[] array;
	private long time;
	public void sort(Integer[] array){
		this.array = array;
		if(array == null || array.length <2){
			return;
		}
		Long start = System.nanoTime();
		sort();
		Long end = System.nanoTime();
		time = end - start;
	}
	protected abstract void sort();

	protected int compare(int i1,int i2){
		compareCount++;
		return array[i1] - array[i2];
	}
	protected void swap(int i1,int i2){
		swapCount++;
		int tmp = array[i1];
		array[i1] = array[i2];
		array[i2] = tmp;
	}

	@Override
	public String toString() {
		return "【"+getClass().getSimpleName()+"】\r\n"+"长度为："+array.length+",耗时："+(time/1000000000)+"s,"+(time/1000000)+"ms\r\n"
			+ "比较次数："+compareCount + "\r\n"+
			"交换次数："+swapCount+"\r\n"+
			"=================================================================";
	}
}
